package com.ketangpai.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ketangpai.admin.dao.CourseDao;
import com.ketangpai.admin.dao.UserCourseDao;
import com.ketangpai.admin.dao.UserDao;
import com.ketangpai.admin.dao.WorkDao;
import com.ketangpai.admin.entity.Course;
import com.ketangpai.admin.entity.User;
import com.ketangpai.admin.entity.UserCourse;
import com.ketangpai.admin.entity.Work;
import com.ketangpai.admin.entity.param.CreateCourseParam;
import com.ketangpai.admin.entity.param.DropCourseParam;
import com.ketangpai.admin.entity.param.UpdateCourseParam;
import com.ketangpai.admin.service.CourseService;
import com.ketangpai.admin.utils.Encrypt;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseImpl extends ServiceImpl<CourseDao, Course> implements CourseService {
    private CourseDao courseDao;
    private UserCourseDao userCourseDao;
    private UserDao userDao;
    private WorkDao workDao;

    public CourseImpl(CourseDao courseDao, UserCourseDao userCourseDao, UserDao userDao, WorkDao workDao) {
        this.courseDao = courseDao;
        this.userCourseDao = userCourseDao;
        this.userDao = userDao;
        this.workDao = workDao;
    }

    @Override
    public Course getCourseById(Integer courseId) {
        return courseDao.selectById(courseId);
    }

    public Course getByCode(String code){
        return courseDao.getByCode(code);
    }
    @Override
    public boolean update(UpdateCourseParam updateCourseParam) {
        return courseDao.updateCourseById(updateCourseParam);
    }

    @Override
    public Course create(Course course) {
        String courseNode=randomString();
        course.setCourseCode(courseNode);
        courseDao.insert(course);
        UserCourse userCourse=new UserCourse(course);

        userCourseDao.insert(userCourse);
        return course;
    }
    public String randomString() {
        // 生成随机[a-z,A-Z]字符串
        String f = RandomStringUtils.randomAlphabetic(1).toUpperCase();
        // 生成指定长度的字母和数字组成的随机组合字符串
        String code = RandomStringUtils.randomAlphanumeric(5);
        while (getByCode(code) != null) {
            code = RandomStringUtils.randomAlphanumeric(5);
        }
        return f + code;
    }

    @Override
    public String delete(DropCourseParam dropCourseParam) {
        try {
            User user=userDao.getById(dropCourseParam.getUserId());
            String password= Encrypt.AESEncrypt(dropCourseParam.getPassword());
            if(password.equals(user.getPassword())){

                userCourseDao.deleteByCourseId(dropCourseParam.getCourseId());
                List<Work> workList=workDao.selectByCourseId(dropCourseParam.getCourseId());
                for (int i=0;i<workList.size();i++){
                    workDao.deleteById(workList.get(i).getId());
                }
                return "删除成功";
            } else return "密码错误";
        }catch (Exception e){
            e.printStackTrace();
            return "删除失败";
        }
    }
}
